package com.bonc.apicreator;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


/**
 * 登录的拦截器
 *
 * @author gaopengyu
 * @version 2016年8月4日
 * @see LoginInterceptor
 * @since
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler)
        throws Exception {
        HttpSession session = request.getSession();

        String username = (String)session.getAttribute("username");
        if (username != null && !"".equals(username)) {
            // 有用户：放行
            return true;
        }
        request.getRequestDispatcher("/login").forward(request, response);

        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView modelAndView)
        throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex)
        throws Exception {

    }

}
